package com.kqzz.tbxt.controller;

import com.kqzz.tbxt.bean.CosTenantServerAddress;
import com.kqzz.tbxt.service.ICosSysPersonnelService;

import java.util.HashMap;
import java.util.Map;

import com.kqzz.tbxt.service.ICosTenantServerAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import com.kqzz.common.api.response.Response;
import com.kqzz.common.api.request.ListRequest;
import com.github.pagehelper.PageInfo;

import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lyx
 * @Description
 * @date 2024/10/18
 */
@Slf4j
@Api(tags = "CosSysRolePersonal控制器")
@RestController
@RequestMapping("/user")
public class CosSysRolePersonalController {

    @Autowired
    private ICosSysPersonnelService sysPersonnelService;

    @Autowired
    private ICosTenantServerAddressService cosTenantServerAddressService;

    /**
     * @return 授权信息
     * @Descripton 系统自用：获取授权用户的信息
     */
    @ApiOperation(value = "系统自用：获取授权用户的信息")
    @GetMapping("/info")
    public Response user(HttpServletRequest request) {
        return new Response().success(sysPersonnelService.user(request));
    }

  @ApiOperation(value = "根据租户code查看对应的服务地址")
  @GetMapping("/tenant/server-address/query")
  public Response queryquery(@RequestParam String cTenantCode) {
    try {
      CosTenantServerAddress cosTenantServerAddress = new CosTenantServerAddress();
      cosTenantServerAddress.setcTenantCode(cTenantCode);
      CosTenantServerAddress result = cosTenantServerAddressService.selectOne(cosTenantServerAddress);
      if (result == null) {
        return new Response().failure("查询为空！请检查数据配置！");
      }
      Map<String, Object> map = new HashMap<>();
      map.put("cServerAddress", result.getcServerAddress());
      return new Response().success(map);
    } catch (Exception e) {
      log.error("保存异常：", e);
      return new Response().failure(e.getMessage());
    }
  }

    @ApiOperation(value = "保存")
    @PostMapping("/tenant/server-address/save")
    public Response add(@ApiParam(value = "请求对象", required = true) @RequestBody CosTenantServerAddress cosTenantServerAddress) {
        try {
            cosTenantServerAddressService.saveNotNull(cosTenantServerAddress);
            return new Response().success();
        } catch (Exception e) {
            log.error("保存异常：", e);
            return new Response().failure();
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping("/tenant/server-address/delete")
    public Response delete(@ApiParam(value = "主键ID {'id':''}", required = true) @RequestBody Map<String, String> map) {
        try {
            cosTenantServerAddressService.deleteLogic(map.get("id"));
            return new Response().success();
        } catch (Exception e) {
            log.error("删除异常：", e);
            return new Response().failure();
        }
    }

    @ApiOperation(value = "更新")
    @PostMapping("/tenant/server-address/update")
    public Response update(@ApiParam(value = "请求对象", required = true) @RequestBody CosTenantServerAddress cosTenantServerAddress) {
        try {
            cosTenantServerAddressService.updateNotNull(cosTenantServerAddress);
            return new Response().success();
        } catch (Exception e) {
            log.error("删除异常：", e);
            return new Response().failure();
        }
    }

    @ApiOperation(value = "查看详情")
    @PostMapping("/detail")
    public Response detail(@ApiParam(value = "主键ID {'id':''}", required = true) @RequestBody Map<String, String> map) {
        CosTenantServerAddress cosTenantServerAddress = cosTenantServerAddressService.selectByKey(map.get("id"));
        return new Response().success(cosTenantServerAddress);
    }

    @ApiOperation(value = "分页列表")
    @PostMapping("/list")
    public Response list(@ApiParam(value = "请求对象", required = true) @RequestBody ListRequest pojo) {
        Example example = new Example(CosTenantServerAddress.class);
        PageInfo<CosTenantServerAddress> pgList = cosTenantServerAddressService.selectAll(example, pojo.getCurrentPage(), pojo.getPageSize());
        return new Response().success(pgList);
    }

}
